package com.cvit.cn.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cvit.cn.bean.Msg;
import com.cvit.cn.bean.ReleaseInformation;
import com.cvit.cn.service.ReleaseInformationService;
import com.cvit.cn.test.UuidUtil;

@Controller
public class ReleaseInformationController {

	@Autowired
	ReleaseInformationService releaseInformationService;
	@Autowired
	HttpServletRequest request;

	/**
	 * 增加发布信息xxxx
	 */
	@RequestMapping(value = "/addReleaseInformation")
	@ResponseBody
	public Msg addReleaseInformation(String title, String content, String releaseTime, String administrators,
			String updataTime, String updataPeople, String quoteStart, String quoteFlag) throws Exception {
		ReleaseInformation addReleaseInformation = new ReleaseInformation();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
		addReleaseInformation.setId(UuidUtil.get32NUMUUID());
		addReleaseInformation.setTitle(title);
		addReleaseInformation.setContent(content);
		addReleaseInformation.setReleaseTime(df.parse(releaseTime));
		addReleaseInformation.setAdministrators(administrators);
		addReleaseInformation.setUpdataTime(df.parse(updataTime));
		addReleaseInformation.setUpdataPeople(updataPeople);
		addReleaseInformation.setQuoteStart(df.parse(quoteStart));
		addReleaseInformation.setQuoteFlag(df.parse(quoteFlag));
		addReleaseInformation.setQuoteType("0");
		if (releaseInformationService.addReleaseInformation(addReleaseInformation) != 0) {
			return Msg.success().add("success", "增加发布信息成功");
		} else {
			return Msg.fail().add("fail", "增加发布信息失败");
		}
	}
	@RequestMapping(value = "/test")
	@ResponseBody
	public Msg test(){
		List<ReleaseInformation> gh=releaseInformationService.getTest();
		return Msg.success();
	}
	/**
	 * 添加发布信息
	 * @throws ParseException 
	 * */
	@RequestMapping(value = "/addRele")
	@ResponseBody
	public Msg addRele(String id,String title,String content) throws Exception{
		String ris = releaseInformationService.addRele(id,title,content);
		return Msg.success().add("ris", ris);
	}
	/**
	 * 添加发布人和结束时间
	 * */
	@RequestMapping(value = "/updateId")
	@ResponseBody
	public Msg updateId(String id,String administrators,String quoteFlag) throws Exception{
		ReleaseInformation ri = new ReleaseInformation();
		ri.setId(id);
		ri.setAdministrators(administrators);
		ri.setQuoteFlag(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(quoteFlag));
		ri.setQuoteType("2");
		releaseInformationService.updateId(ri);
		return Msg.success().add("success", "成功了");
		
	}
	/**
	 * 根据quote_type=0查询查询已发布订单
	 * */
	@RequestMapping(value = "/getTypeZero")
	@ResponseBody
	public Msg getTypeZero() {
		List<ReleaseInformation> rf=releaseInformationService.getTypeZero();
		return Msg.success().add("rf", rf);
		
	}
	/**
	 * 根据quote_type=null查询查询未发布订单
	 * */
	@RequestMapping(value = "/getTypeNull")
	@ResponseBody
	public Msg getTypeNull() {
		List<ReleaseInformation> rf=releaseInformationService.getTypeNull();
		return Msg.success().add("rf", rf);
		
	}
	/**
	 * 查询发布全部信息
	 * */
	@RequestMapping(value = "/getType")
	@ResponseBody
	public Msg getType(){
		List<ReleaseInformation> rf=releaseInformationService.getTypeOne();
		return Msg.success().add("rf", rf);
		
	}
	
	/**
	 * 如果数据库里的时间<=当前时间就赋值quote_type=1
	 * */
	/*@RequestMapping(value = "/getTypeOne")
	@ResponseBody
	public Msg getTypeOne() throws Exception{
		List<ReleaseInformation> rf=releaseInformationService.getType();
		ReleaseInformation rel = new ReleaseInformation();
		for(int i=0;i<=rf.size();i++){
			rel.setId(rf.get(i).getId());
			rel.setQuoteType("1");
			//releaseInformationService.updateReleaseInformationForQuoteType(rel);
		}
		return Msg.success();
	}*/
	/*
	 * 如果数据库里的时间<=当前时间就赋值quote_type=1
	 */
	@RequestMapping(value = "/getTypeOneTest")
	@ResponseBody
	public Msg getTypeOneTest() {
		int i = releaseInformationService.updateReleaseInformationForQuoteType();
		if(i!=0){
			return Msg.success();
		}else{
			return Msg.fail();
		}
	}
	/**
	 * 删除发布信息
	 */
	@RequestMapping(value = "/delReleaseInformation")
	@ResponseBody
	public Msg delReleaseInformation(String id) {
		String[] ids = id.split(",");
		if (releaseInformationService.delReleaseInformation(ids) != 0) {
			return Msg.success().add("success", "删除发布信息成功");
		} else {
			return Msg.fail().add("fail", "删除发布信息失败");
		}
	}

	/**
	 * 查询发布信息
	 */
	@RequestMapping(value = "/selectReleaseInformation")
	@ResponseBody
	public Msg selectReleaseInformation() {
		// 查询发布信息 已发布 返回多条数据 每一条是一个ReleaseInformation对象 放到list里
		try {
			List<ReleaseInformation> list = releaseInformationService.selectReleaseInformation();
			// 提示查询成功并给前台返回查询出的数据
			return Msg.success().add("success", list);
		} catch (Exception e) {
			return Msg.fail().add("fail", "查询发布信息失败");
		}
	}

	/**
	 * 修改发布信息
	 */
	@RequestMapping(value = "/updateReleaseInformation")
	@ResponseBody
	public Msg updateReleaseInformation(String id, String title, String content, String releaseTime,
			String administrators, String updataTime, String updataPeople, String quoteStart, String quoteFlag)
			throws Exception {
		ReleaseInformation updateReleaseInformation = new ReleaseInformation();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
		updateReleaseInformation.setId(id);
		updateReleaseInformation.setTitle(title);
		updateReleaseInformation.setContent(content);
		updateReleaseInformation.setReleaseTime(df.parse(releaseTime));
		updateReleaseInformation.setAdministrators(administrators);
		updateReleaseInformation.setUpdataTime(df.parse(updataTime));
		updateReleaseInformation.setUpdataPeople(updataPeople);
		updateReleaseInformation.setQuoteStart(df.parse(quoteStart));
		updateReleaseInformation.setQuoteFlag(df.parse(quoteFlag));
		updateReleaseInformation.setQuoteType("0");
		if (releaseInformationService.updateReleaseInformation(updateReleaseInformation) != 0) {
			return Msg.success().add("success", "修改发布信息成功");
		} else {
			return Msg.fail().add("fail", "修改发布信息失败");
		}

	}

	/**
	 * 假删除发布信息
	 */

	@RequestMapping(value = "/delReleaseInformationFalse")
	@ResponseBody
	public Msg delReleaseInformationFalse(String id) {
		String[] ids = id.split(",");
		if (releaseInformationService.delReleaseInformationFalse(ids) != 0) {
			return Msg.success().add("success", "假删除发布信息成功");
		} else {
			return Msg.fail().add("fail", "假删除发布信息失败");
		}
	}
	/***
	 * 根据订单id查开始时间和结束时间
	 * */
	@RequestMapping("/getReleaseIdTime")
	@ResponseBody
	public Msg getReleaseIdTime(@RequestParam("id")String id){
		ReleaseInformation re=releaseInformationService.getReleaseIdTime(id);
		return Msg.success().add("Time", re);
	}
	/***
	 * 查未处理状态
	 * */
	@RequestMapping("/getReleaseIdSupplierType")
	@ResponseBody
	public Msg getReleaseIdSupplierType(){
		List<ReleaseInformation> re=releaseInformationService.getReleaseIdSupplierType();
		return Msg.success().add("Time", re);
	}
	/***
	 * 查的待处理订单
	 * */
	@RequestMapping("/getReleaseIdSupplierTypeOne")
	@ResponseBody
	public Msg getReleaseIdSupplierTypeOne(){
		List<ReleaseInformation> re=releaseInformationService.getReleaseIdSupplierTypeOne();
		return Msg.success().add("Time", re);
	}
}
